/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import componenten.Database;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import model.Masterclass;

/**
 *
 * @author daveronvanderham
 */
public class MasterclassOverzicht extends javax.swing.JPanel {

    /**
     * Creates new form MasterclassOverzicht
     */
    public MasterclassOverzicht() {
        initComponents();
        update_table();
    }

    private void update_table() {

        String[] kolommen = {" M Bijnaam", "Datum", "Tijd", "id"};
        DefaultTableModel model = new DefaultTableModel(kolommen, 0);

        try {
            String sql = "SELECT * FROM masterclass where masterclass_datum >= CURDATE() and (status = 'actief' or status = 'voltooid');";
            ResultSet resultaat = Database.executeSelectQuery(sql);

            while (resultaat.next()) {
                String id = resultaat.getString("masterclass_id");

                Masterclass tabelVulling = new Masterclass(id);
                model.addRow(tabelVulling.getInformatie());
            }
        } catch (SQLException e) {
            System.out.println(e);

        }

        tblMasterclass.setModel(model);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblMasterclass = new javax.swing.JTable();
        masterclassToevoegen = new javax.swing.JButton();
        btnBeheren = new javax.swing.JButton();
        btnVernieuwen = new javax.swing.JButton();

        setBackground(new java.awt.Color(112, 112, 112));
        setPreferredSize(new java.awt.Dimension(800, 400));
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                formMouseEntered(evt);
            }
        });

        tblMasterclass.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblMasterclass.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblMasterclassMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblMasterclass);

        masterclassToevoegen.setText(" toevoegen");
        masterclassToevoegen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                masterclassToevoegenActionPerformed(evt);
            }
        });

        btnBeheren.setText("Beheren");
        btnBeheren.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBeherenActionPerformed(evt);
            }
        });

        btnVernieuwen.setText("Vernieuwen");
        btnVernieuwen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVernieuwenActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(btnVernieuwen)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, btnBeheren, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, masterclassToevoegen, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .add(18, 18, 18)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 677, Short.MAX_VALUE)
                .add(7, 7, 7))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(masterclassToevoegen)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(btnBeheren)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(btnVernieuwen)))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void tblMasterclassMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblMasterclassMouseClicked

    }//GEN-LAST:event_tblMasterclassMouseClicked

    private void masterclassToevoegenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_masterclassToevoegenActionPerformed
        // TODO add your handling code here:
        new MasterclassToevoegen().setVisible(true);
    }//GEN-LAST:event_masterclassToevoegenActionPerformed

    private void btnBeherenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBeherenActionPerformed
       int geselecteerdeRij = tblMasterclass.getSelectedRow();
        if (geselecteerdeRij == -1) {
            JOptionPane.showMessageDialog(null, "Selecteer eerst een Masterclass voordat u kunt Bijwerken");
        } else {
            String id = (String)tblMasterclass.getModel().getValueAt(geselecteerdeRij, 3);

            new MasterclassBeheren(id).setVisible(true);
        }
    }//GEN-LAST:event_btnBeherenActionPerformed

    private void formMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseEntered
        // TODO add your handling code here:
    }//GEN-LAST:event_formMouseEntered

    private void btnVernieuwenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVernieuwenActionPerformed
        this.update_table();
    }//GEN-LAST:event_btnVernieuwenActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBeheren;
    private javax.swing.JButton btnVernieuwen;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton masterclassToevoegen;
    private javax.swing.JTable tblMasterclass;
    // End of variables declaration//GEN-END:variables
}
